package smartclass.present;

import java.util.ArrayList;

import smartclass.dao.StudentDataDao;
import smartclass.dao.TeacherDataDao;
import smartclass.data.TableItem;
import smartclass.entity.StudentInfo;
import smartclass.entity.TeacherInfo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.example.smartclass.R;

public class RegisterActivity extends Activity {
	private Button btnRegister, btnCancel;
	private EditText edtId, edtName, edtUsername, edtPassword, edtPassGain,
			edtPosition, edtPhoneNumber;
	private TextView tvPosition;
	private RadioGroup typeGroup, genderGroup;
	private ArrayList<TeacherInfo> allTeacher;
	private ArrayList<StudentInfo> allStudent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_activity);
		btnRegister = (Button) findViewById(R.id.btnOKregister);
		btnCancel = (Button) findViewById(R.id.btnCancelregister);
		edtId = (EditText) findViewById(R.id.edtRegisterId);
		edtName = (EditText) findViewById(R.id.edtRegisterName);
		edtUsername = (EditText) findViewById(R.id.edtRegisteruser);
		edtPassword = (EditText) findViewById(R.id.edtRegisterpassword);
		edtPassGain = (EditText) findViewById(R.id.edtRegisterRepassword);
		edtPosition = (EditText) findViewById(R.id.edtRegisterPosition);
		typeGroup = (RadioGroup) findViewById(R.id.radioType);
		genderGroup = (RadioGroup) findViewById(R.id.radioGender);
		edtPhoneNumber = (EditText) findViewById(R.id.edtRegisterNumberPhone);
		tvPosition = (TextView) findViewById(R.id.tvPosition);

		typeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (checkedId == R.id.teacherRadio) {
					tvPosition.setText("Position");

				} else {
					tvPosition.setText("Name Class");
				}
			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				TableItem.displayNotification(getBaseContext(),
						"Register is canceled!");
				finish();
			}
		});

		btnRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				switch (typeGroup.getCheckedRadioButtonId()) {
				case R.id.teacherRadio:
					TeacherInfo teacher = new TeacherInfo();
					TeacherDataDao teacherDao = TeacherDataDao.getInstance();
					String item = edtId.getText().toString();
					if (!item.isEmpty()) {
						teacher.setId(Integer.parseInt(item));
						//TableItem.displayNotification(getBaseContext(), teacher.getId()+" ");
						allTeacher = teacherDao.getAllTeacher();
						TableItem.displayNotification(getBaseContext(), allTeacher.size()+" ");
						for (TeacherInfo teacherInfo : allTeacher) {
							
							if (String.valueOf(teacherInfo.getId())
									.equals(item)) {
								TableItem.displayNotification(getBaseContext(),
										"ID existed\nEnter difference ID");
							} else {							
								teacher.setId(Integer.parseInt(item));
								
							}
						}
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your ID");
						edtId.requestFocus();
						return;
					}
					item = edtName.getText().toString();
					if (!item.isEmpty()) {
						teacher.setName(item);
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your Name");
						edtName.requestFocus();
						return;
					}

					item = edtUsername.getText().toString();
					if (!item.isEmpty()) {
						teacher.setUserName(item);
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your User Name");
						edtUsername.requestFocus();
						return;
					}
					switch (genderGroup.getCheckedRadioButtonId()) {
					case R.id.maleRadio:
						teacher.setGender("Male");

						break;
					case R.id.femaleRadio:
						teacher.setGender("Female");

						break;
					default:
						break;
					}

					item = edtPassword.getText().toString();
					if (!item.isEmpty()) {

					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your PassWord");
						edtPassword.requestFocus();
						return;
					}

					item = edtPassGain.getText().toString();
					if (!item.isEmpty()) {
						if (!item.equals(edtPassword.getText().toString())) {
							TableItem.displayNotification(getBaseContext(),
									"Password is not available");
							return;
						} else {
							teacher.setPassword(item);
						}
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Repeat Your PassWord");
						edtPassGain.requestFocus();
						return;
					}
					//teacher.setPhoneNumber(edtPhoneNumber.getText().toString());
					if (teacherDao.addTeacher(teacher)) {
						TableItem.displayNotification(getBaseContext(),
								"Register success");
						finish();
					}

					break;
				case R.id.studentRadio:

					StudentInfo student = new StudentInfo();
					StudentDataDao studentDao = StudentDataDao.getInstance();

					String item1 = edtId.getText().toString();
					if (!item1.isEmpty()) {
						allStudent = studentDao.getAllStudent();
						for (StudentInfo studentInfo : allStudent) {
							if (String.valueOf(studentInfo.getId()).equals(
									item1)) {
								TableItem.displayNotification(getBaseContext(),
										"ID existed\nEnter difference ID");
							} else {
								student.setId(Integer.parseInt(item1));
							}
						}
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your ID");
						edtId.requestFocus();
						return;
					}
					item1 = edtName.getText().toString();
					if (!item1.isEmpty()) {
						student.setName(item1);
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your Name");
						edtName.requestFocus();
						return;
					}

					item1 = edtUsername.getText().toString();
					if (!item1.isEmpty()) {
						student.setUserName(item1);
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your User Name");
						edtUsername.requestFocus();
						return;
					}
					switch (genderGroup.getCheckedRadioButtonId()) {
					case R.id.maleRadio:
						student.setGender("Male");

						break;
					case R.id.femaleRadio:
						student.setGender("Female");

						break;
					default:
						break;
					}

					item1 = edtPassword.getText().toString();
					if (!item1.isEmpty()) {

					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Enter Your PassWord");
						edtPassword.requestFocus();
						return;
					}

					item1 = edtPassGain.getText().toString();
					if (!item1.isEmpty()) {
						if (!item1.equals(edtPassword.getText().toString())) {
							TableItem.displayNotification(getBaseContext(),
									"Password is not available");
							return;
						} else {
							student.setPassword(item1);
						}
					} else {
						TableItem.displayNotification(getBaseContext(),
								"Please Repeat Your PassWord");
						edtPassGain.requestFocus();
						return;
					}

					student.setPhoneNumber(edtPhoneNumber.getText().toString());
					student.setClassName(edtPosition.getText().toString());
					if (studentDao.addStudent(student)) {
						TableItem.displayNotification(getBaseContext(),
								"Register success");
						finish();
					}

					break;
				default:
					break;
				}
			}
		});
	}
}
